package com.nefu.huiyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nefu.huiyi.entity.MedicalCompany;
import com.nefu.huiyi.entity.MedicalCompanyPolicy;
import com.nefu.huiyi.exception.BusinessException;
import com.nefu.huiyi.mapper.IMedicalCompanyMapper;
import com.nefu.huiyi.mapper.IMedicalCompanyPolicyMapper;
import com.nefu.huiyi.service.IMedicalCompanyPolicyService;
import com.nefu.huiyi.service.IMedicalCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Service
public class MedicalCompanyServiceImpl extends ServiceImpl<IMedicalCompanyMapper, MedicalCompany> implements IMedicalCompanyService {
    // 继承ServiceImpl后，大部分通用业务方法已实现

    @Autowired
    private IMedicalCompanyPolicyMapper policyMapper;

    @Override
    @Transactional
    public boolean removeById(Serializable id) {
        QueryWrapper<MedicalCompanyPolicy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id", id);
        queryWrapper.eq("status", 1);

        long unprocessedPolicyCount = policyMapper.selectCount(queryWrapper);

        if (unprocessedPolicyCount > 0) {
            throw new BusinessException("该公司下有未处理的政策，无法删除");
        }

        return super.removeById(id);
    }

    public List<MedicalCompany> listAllForSelection(){
        QueryWrapper<MedicalCompany> queryWrapper = new QueryWrapper<>();
        // 使用 select() 方法指定只查询 id 和 company_name
        queryWrapper.select("id","company_name");
        return this.list(queryWrapper);
    }
}
